#if VS_TESSELLATION

///////////////// hull shader //////////////////

HS_CONSTANT_DATA_OUTPUT ZPassConstantsHS(InputPatch<vert2fragZ, 3> p, uint PatchID : SV_PrimitiveID)
{
	const float3 vP0 = p[0].vView.xyz + g_VS_WorldViewPos.xyz;
	const float3 vP1 = p[1].vView.xyz + g_VS_WorldViewPos.xyz;
	const float3 vP2 = p[2].vView.xyz + g_VS_WorldViewPos.xyz;
	HS_CONSTANT_DATA_OUTPUT output = CommonConstantsHS(vP0, vP1, vP2, g_VS_WorldViewPos.xyz, g_VS_WorldViewPos.xyz, PatchID);

	return output;
}

[domain("tri")]
[partitioning("fractional_odd")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(3)]
[patchconstantfunc("ZPassConstantsHS")]
[maxtessfactor(MAX_TESS_FACTOR)]
HS_CONTROL_POINT_OUTPUT_ZPASS Common_ZPassHS(InputPatch<vert2fragZ, 3> inputPatch, uint uCPID : SV_OutputControlPointID)
{
	HS_CONTROL_POINT_OUTPUT_ZPASS	output = (HS_CONTROL_POINT_OUTPUT_ZPASS)0;

	output.controlPoint.vView   = inputPatch[uCPID].vView;
	output.controlPoint.vNormal = inputPatch[uCPID].vNormal.xyz;
	output.controlPoint.vBaseTC = inputPatch[uCPID].baseTC;
#if (!TEMP_TERRAIN || VS_ALPHABLEND) && !%GRASS
	output.vTangent             = inputPatch[uCPID].vTangent;
	output.vBinormal            = inputPatch[uCPID].vBinormal.xyz;
#endif
#if %BLENDLAYER
	output.Color                = inputPatch[uCPID].Color;
#endif
	output.vZInfo               = inputPatch[uCPID].ZInfo;

	return output;
}

///////////////// domain shader //////////////////

void FillEvalInput(in HS_CONTROL_POINT_OUTPUT_ZPASS triPatch[3], in HS_CONSTANT_DATA_OUTPUT hsConstData, in float3 vBaryCoords, inout CommonEvaluationInputDS evalInput)
{
	evalInput.vBaryCoords = vBaryCoords;
	FillEvalInputControlPoint(triPatch[0].controlPoint, triPatch[1].controlPoint, triPatch[2].controlPoint, hsConstData, evalInput);
	evalInput.bRelativePos = true;
#if (!TEMP_TERRAIN || VS_ALPHABLEND) && !%GRASS
	FillEvalInputTangentBinormal(triPatch[0].vTangent, triPatch[1].vTangent, triPatch[2].vTangent, triPatch[0].vBinormal.xyzz, triPatch[1].vBinormal.xyzz, triPatch[2].vBinormal.xyzz, evalInput);
#endif
#if %BLENDLAYER
	FillEvalInputColor(triPatch[0].Color, triPatch[1].Color, triPatch[2].Color, evalInput);
#endif
	evalInput.vViewPos = g_VS_WorldViewPos.xyz;
}

void ProcessEvalOutput(in CommonEvaluationOutputDS evalOutput, inout vert2fragZ dsOutput)
{
	dsOutput.baseTC = evalOutput.vBaseTC;
#if (!TEMP_TERRAIN || VS_ALPHABLEND) && !%GRASS
	dsOutput.vTangent = evalOutput.vTangent;
	dsOutput.vBinormal = evalOutput.vBinormal.xyz;
#endif
#if %BLENDLAYER
	dsOutput.Color = evalOutput.vColor;
#endif
	dsOutput.vNormal.xyz = evalOutput.vNormal;
	dsOutput.HPosition = mul(g_VS_ViewProjZeroMatr, float4(evalOutput.vPos.xyz, 1));
}

[domain("tri")]
vert2fragZ Common_ZPassDS( HS_CONSTANT_DATA_OUTPUT hsConstData, float3 vBaryCoords : SV_DomainLocation, const OutputPatch<HS_CONTROL_POINT_OUTPUT_ZPASS, 3> TrianglePatch )
{
	vert2fragZ output = (vert2fragZ)0;

	CommonEvaluationInputDS evalInput = (CommonEvaluationInputDS)0;
	FillEvalInput(TrianglePatch, hsConstData, vBaryCoords, evalInput);

	CommonEvaluationOutputDS evalOutput;
	Evaluate(evalInput, evalOutput);
	ProcessEvalOutput(evalOutput, output);

	// Output depth
	output.ZInfo = EvalVec(TrianglePatch[0].vZInfo, TrianglePatch[1].vZInfo, TrianglePatch[2].vZInfo, vBaryCoords);
	output.ZInfo.x = output.HPosition.w * g_VS_NearFarClipDist.w;
#if %_RT_NEAREST
	output.ZInfo.x *= g_VS_NearFarClipDist.z;
#endif

	return output;
}

#endif
